#include <gtest/gtest.h>
#include "risk_manager.h"

using namespace hft;

class RiskManagerTest : public ::testing::Test {
protected:
    void SetUp() override {
        // 设置测试环境
        RiskConfig config;
        
        // 设置全局风控限制
        config.global_limits.max_position_value = 1000000.0;
        config.global_limits.max_daily_loss = 50000.0;
        config.global_limits.max_leverage = 3.0;
        config.global_limits.max_order_size = 100;
        config.global_limits.max_daily_orders = 1000;
        config.global_limits.max_order_value = 100000.0;
        config.global_limits.max_position_concentration = 0.3;
        
        // 设置合约风控限制
        ContractRiskLimits if_limits;
        if_limits.max_position_value = 500000.0;
        if_limits.max_daily_loss = 20000.0;
        if_limits.max_leverage = 2.0;
        if_limits.max_order_size = 50;
        if_limits.max_daily_orders = 500;
        if_limits.max_order_value = 50000.0;
        if_limits.price_limit_percent = 0.1;
        config.contract_limits["IF2403"] = if_limits;
        
        risk_manager_ = std::make_unique<RiskManager>(config);
        
        // 设置账户余额和合约价格
        risk_manager_->setAccountBalance("test_account", 100000.0);
        risk_manager_->setContractPrice("IF2403", 7500.0);
    }

    std::unique_ptr<RiskManager> risk_manager_;
};

// 测试订单风控检查
TEST_F(RiskManagerTest, CheckOrder) {
    // 创建一个正常订单
    Order order;
    order.symbol = "IF2403";
    order.side = OrderSide::Buy;
    order.type = OrderType::Limit;
    order.price = 7500.0;
    order.volume = 10;
    order.account = "test_account";
    order.strategy_id = "test_strategy";

    // 检查订单
    auto result = risk_manager_->checkOrder(order);
    EXPECT_TRUE(result.passed);
    EXPECT_TRUE(result.reason.empty());

    // 创建一个超过订单数量限制的订单
    order.volume = 60;
    result = risk_manager_->checkOrder(order);
    EXPECT_FALSE(result.passed);
    EXPECT_FALSE(result.reason.empty());
    EXPECT_EQ(result.reason, "订单数量超过合约限制");

    // 创建一个超过订单金额限制的订单
    order.volume = 10;
    order.price = 10000.0;
    result = risk_manager_->checkOrder(order);
    EXPECT_TRUE(result.passed); // 10 * 10000 = 100000，不超过全局限制

    order.volume = 20;
    result = risk_manager_->checkOrder(order);
    EXPECT_FALSE(result.passed); // 20 * 10000 = 200000，超过全局限制
    EXPECT_EQ(result.reason, "订单金额超过全局限制");

    // 创建一个超过价格涨跌幅限制的订单
    order.volume = 10;
    order.price = 8500.0; // 超过7500 * 1.1 = 8250
    result = risk_manager_->checkOrder(order);
    EXPECT_FALSE(result.passed);
    EXPECT_EQ(result.reason, "订单价格超过涨跌幅限制");
}

// 测试持仓风控检查
TEST_F(RiskManagerTest, CheckPosition) {
    // 创建一个正常持仓
    Position position;
    position.symbol = "IF2403";
    position.account = "test_account";
    position.strategy_id = "test_strategy";
    position.side = PositionSide::Long;
    position.volume = 10;
    position.avg_price = 7500.0;
    position.last_price = 7500.0;
    position.position_value = 75000.0;
    position.margin = 37500.0;
    position.leverage = 2.0;

    // 检查持仓
    auto result = risk_manager_->checkPosition(position);
    EXPECT_TRUE(result.passed);
    EXPECT_TRUE(result.reason.empty());

    // 创建一个超过持仓市值限制的持仓
    position.volume = 100;
    position.position_value = 750000.0;
    result = risk_manager_->checkPosition(position);
    EXPECT_FALSE(result.passed);
    EXPECT_EQ(result.reason, "持仓市值超过合约限制");

    // 创建一个超过杠杆率限制的持仓
    position.volume = 10;
    position.position_value = 75000.0;
    position.margin = 15000.0;
    position.leverage = 5.0;
    result = risk_manager_->checkPosition(position);
    EXPECT_FALSE(result.passed);
    EXPECT_EQ(result.reason, "杠杆率超过全局限制");
}

// 测试风控配置加载和保存
TEST_F(RiskManagerTest, ConfigLoadAndSave) {
    // 获取当前配置
    const auto& config = risk_manager_->getConfig();
    
    // 验证全局限制
    EXPECT_EQ(config.global_limits.max_position_value, 1000000.0);
    EXPECT_EQ(config.global_limits.max_daily_loss, 50000.0);
    EXPECT_EQ(config.global_limits.max_leverage, 3.0);
    EXPECT_EQ(config.global_limits.max_order_size, 100);
    
    // 验证合约限制
    auto it = config.contract_limits.find("IF2403");
    ASSERT_NE(it, config.contract_limits.end());
    EXPECT_EQ(it->second.max_position_value, 500000.0);
    EXPECT_EQ(it->second.max_leverage, 2.0);
    EXPECT_EQ(it->second.max_order_size, 50);
    
    // 更新配置
    RiskConfig new_config = config;
    new_config.global_limits.max_position_value = 2000000.0;
    new_config.global_limits.max_leverage = 4.0;
    
    ContractRiskLimits new_limits;
    new_limits.max_position_value = 800000.0;
    new_limits.max_leverage = 3.0;
    new_limits.max_order_size = 80;
    new_config.contract_limits["IC2403"] = new_limits;
    
    risk_manager_->updateConfig(new_config);
    
    // 验证更新后的配置
    const auto& updated_config = risk_manager_->getConfig();
    EXPECT_EQ(updated_config.global_limits.max_position_value, 2000000.0);
    EXPECT_EQ(updated_config.global_limits.max_leverage, 4.0);
    
    auto new_it = updated_config.contract_limits.find("IC2403");
    ASSERT_NE(new_it, updated_config.contract_limits.end());
    EXPECT_EQ(new_it->second.max_position_value, 800000.0);
    EXPECT_EQ(new_it->second.max_leverage, 3.0);
    EXPECT_EQ(new_it->second.max_order_size, 80);
}

// 测试日交易统计更新
TEST_F(RiskManagerTest, DailyStatsUpdate) {
    // 创建一个成交
    Trade trade;
    trade.trade_id = "T1";
    trade.order_id = "O1";
    trade.symbol = "IF2403";
    trade.side = OrderSide::Buy;
    trade.price = 7600.0; // 高于当前价格7500.0，产生亏损
    trade.volume = 10;
    trade.account = "test_account";
    trade.strategy_id = "test_strategy";

    // 更新日交易统计
    risk_manager_->updateDailyStats(trade);
    
    // 创建一个订单
    Order order;
    order.symbol = "IF2403";
    order.side = OrderSide::Buy;
    order.type = OrderType::Limit;
    order.price = 7500.0;
    order.volume = 10;
    order.account = "test_account";
    order.strategy_id = "test_strategy";
    
    // 检查订单
    auto result = risk_manager_->checkOrder(order);
    EXPECT_TRUE(result.passed);
    
    // 重置日交易统计
    risk_manager_->resetDailyStats();
    
    // 再次检查订单
    result = risk_manager_->checkOrder(order);
    EXPECT_TRUE(result.passed);
}

// 测试账户余额检查
TEST_F(RiskManagerTest, AccountBalanceCheck) {
    // 创建一个订单
    Order order;
    order.symbol = "IF2403";
    order.side = OrderSide::Buy;
    order.type = OrderType::Limit;
    order.price = 7500.0;
    order.volume = 10;
    order.account = "test_account";
    order.strategy_id = "test_strategy";
    
    // 检查订单
    auto result = risk_manager_->checkOrder(order);
    EXPECT_TRUE(result.passed);
    
    // 设置较低的账户余额
    risk_manager_->setAccountBalance("test_account", 5000.0);
    
    // 再次检查订单
    result = risk_manager_->checkOrder(order);
    EXPECT_TRUE(result.passed); // 5000 > 7500 * 10 * 0.1 = 750
    
    // 设置更低的账户余额
    risk_manager_->setAccountBalance("test_account", 500.0);
    
    // 再次检查订单
    result = risk_manager_->checkOrder(order);
    EXPECT_FALSE(result.passed); // 500 < 7500 * 10 * 0.1 = 750
    EXPECT_EQ(result.reason, "账户余额不足");
}